import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_roujiaosuo/flutter_bloc/requests/repository/supply_repository.dart';
import './bloc.dart';

class SupplyBloc extends Bloc<SupplyEvent, SupplyState> {
  final SupplyRepository supplyRepository;
  SupplyBloc({@required this.supplyRepository})
      : assert(supplyRepository != null),
        super();

  @override
  SupplyState get initialState => InitialSupplyState();

  @override
  Stream<SupplyState> mapEventToState(
    SupplyEvent event,
  ) async* {
    // TODO: Add Logic
    if (event is FetchSupplyListDataEvent) {
      yield LoadingSupplyState();
      try {
        yield LoadedSupplyState(
            supply: await supplyRepository.getSupplyListData(event.map));
      } catch (e) {
        yield ErrorSupplyState();
      }
    }
  }
}
